//
//  WeatherCustomerView.m
//  CPICApp
//
//  Created by zhiwei li on 13-3-27.
//  Copyright (c) 2013年 lizhiwei. All rights reserved.
//

#import "WeatherCustomerView.h"
#import "Config.h"
#import "NSString+SBJson.h"
#import "NSObject+SBJson.h"
#import "TKAlertCenter.h"

#define Weather_Url @"http://m.weather.com.cn/data/101280601.html"

@implementation WeatherCustomerView
@synthesize progress;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        int iphone5Height;
        if (IS_IPHONE_5) {
            iphone5Height=50;
        }
        else
            
        {
            iphone5Height=0;
        }

        WeatherImage = [[UIImageView alloc]init];
        WeatherImage.image = [UIImage imageNamed:@"a_1.png"];
        WeatherImage.frame = CGRectMake(170, 210+iphone5Height, WeatherImage.image.size.width, WeatherImage.image.size.height);
        [self addSubview:WeatherImage];
        [WeatherImage release];
        
        NSDictionary * dictCustomerInfo =[[NSUserDefaults standardUserDefaults] dictionaryForKey:Dict_Weather_Info];
        
        UILabel *cityname = [[UILabel alloc]initWithFrame:CGRectMake(43, 178+iphone5Height, 70, 30)];
        cityname.text = @"深 圳";
        cityname.font = [UIFont boldSystemFontOfSize:20.0];
        cityname.backgroundColor = [UIColor clearColor];
        cityname.textColor = [UIColor whiteColor];
        [self addSubview:cityname];
        [cityname release];
        
        temp = [[UILabel alloc]initWithFrame:CGRectMake(43, 198+iphone5Height, 170, 50)];
        if (dictCustomerInfo!=nil)
        {
            if ([dictCustomerInfo valueForKey:@"Temp"])
            {
                temp.text = [dictCustomerInfo valueForKey:@"Temp"];
            }
            
        }
        temp.font = [UIFont boldSystemFontOfSize:23.0];
        temp.backgroundColor = [UIColor clearColor];
        temp.textColor = [UIColor whiteColor];
        [self addSubview:temp];
        [temp release];
        
        weather = [[UILabel alloc]initWithFrame:CGRectMake(43, 228+iphone5Height, 170, 50)];
        if (dictCustomerInfo!=nil)
        {
            if ([dictCustomerInfo valueForKey:@"Weather"])
            {
                weather.text = [dictCustomerInfo valueForKey:@"Weather"];
            }
            
        }
        weather.font = [UIFont boldSystemFontOfSize:20.0];
        weather.backgroundColor = [UIColor clearColor];
        weather.textColor = [UIColor whiteColor];
        [self addSubview:weather];
        [weather release];
        
        xc = [[UILabel alloc]initWithFrame:CGRectMake(43, 258+iphone5Height, 170, 50)];
        if (dictCustomerInfo!=nil)
        {
            if ([dictCustomerInfo valueForKey:@"Wash"])
            {
                xc.text = [dictCustomerInfo valueForKey:@"Wash"];
            }
            
        }
        xc.font = [UIFont boldSystemFontOfSize:20.0];
        xc.backgroundColor = [UIColor clearColor];
        xc.textColor = [UIColor whiteColor];
        [self addSubview:xc];
        [xc release];
        
        Date = [[UILabel alloc]initWithFrame:CGRectMake(170, 140+iphone5Height, 170, 50)];
        if (dictCustomerInfo!=nil)
        {
            if ([dictCustomerInfo valueForKey:@"Date"])
            {
                Date.text = [dictCustomerInfo valueForKey:@"Date"];
            }
            
        }
        Date.font = [UIFont boldSystemFontOfSize:18.0];
        Date.backgroundColor = [UIColor clearColor];
        Date.textColor = [UIColor whiteColor];
        [self addSubview:Date];
        [Date release];
        
        [self setWeatherInfoRequest];
    }
    return self;
}

-(void)setWeatherInfoRequest//向中国气象局平台发送深圳天气预报请求报文
{
    NSString *strUrl=[NSString stringWithFormat:@"%@",Weather_Url];
    NSString *strUrlEncod = [strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];//字符串编码转换
    NSURL *myurl = [NSURL URLWithString:strUrlEncod];
    NSLog(@"请求报文为:%@",strUrl);
    requestWeather=[ASIHTTPRequest requestWithURL:myurl];
    requestWeather.tag=1001;
    
    [requestWeather setDelegate:self];
    [requestWeather setDidFailSelector:@selector(ResponseFail:)];
    [requestWeather setDidFinishSelector:@selector(ResonseSuccess:)];
    [requestWeather startAsynchronous];
    
    showProgress = YES;
    if (showProgress) 
    {
        progress= [[MBProgressHUD alloc]initWithView:self];
        [self addSubview:progress];
        [self bringSubviewToFront:progress];
        progress.labelText = @"加载中...";
        [progress show:NO];
        showProgress = NO;
        [progress setHidden:YES];
    };
}

- (void)ResonseSuccess:(ASIHTTPRequest *)request
{
    if (!showProgress)
    {
        [progress removeFromSuperview];
        progress = nil;
    }
    NSString *responseString = [request responseString];
    NSLog(@"responseString %@",responseString);
    NSMutableDictionary *dictResponse=[responseString JSONValue];
    NSLog(@"dictResponse  %@",dictResponse);
    if ([dictResponse count]==0) {
        return;
    }
    
    NSMutableDictionary *jsonoSubObj = [dictResponse objectForKey:@"weatherinfo"];
    
    //天气
    weather.text = [[NSString alloc] initWithFormat:@"%@",[jsonoSubObj objectForKey:@"weather1"]];
    //温度
    temp.text = [[NSString alloc] initWithFormat:@"%@",[jsonoSubObj objectForKey:@"temp1"]];
    //日期
    Date.text = [[NSString alloc] initWithFormat:@"%@",[jsonoSubObj objectForKey:@"date_y"]];
    //洗车指数
    NSString * xcstr = [[NSString alloc] initWithFormat:@"洗车:%@",[jsonoSubObj objectForKey:@"index_xc"]];
    xc.text = xcstr;
    NSLog(@"============%@",Date.text);
    
    NSDictionary *dictCustomerInfo=[[NSDictionary alloc]initWithObjectsAndKeys:weather.text,@"Weather",temp.text,@"Temp",Date.text,@"Date",xc.text,@"Wash", nil];
    NSUserDefaults *myDefaults=[NSUserDefaults standardUserDefaults];
    [myDefaults setObject:dictCustomerInfo forKey:Dict_Weather_Info];
    
    //天气图片
    NSString * imgname = [[NSString alloc] initWithFormat:@"%@",
                          [jsonoSubObj objectForKey:@"img1"]];
    NSString *pictureName = imgname;
	pictureName = [NSString stringWithFormat:@"a_%@.png", pictureName];
	[WeatherImage setImage:[UIImage imageNamed:pictureName]];
    
    [[TKAlertCenter defaultCenter] postAlertWithMessage:@"天气信息已更新!"];
    return;
}

- (void)ResponseFail:(ASIHTTPRequest *)request
{
    if (!showProgress)
    {
        [progress removeFromSuperview];
        progress = nil;
    }
    NSLog(@"服务器连接失败!");
    [[TKAlertCenter defaultCenter] postAlertWithMessage:@"天气信息获取失败,服务器连接失败!"];
    return;
}

-(void) dealloc
{
    [progress release];
    [WeatherImage release];
    [temp release];
    [weather release];
    [xc release];
    [Date release];
    requestWeather.delegate=nil;
    [super dealloc];

}

@end
